import java.util.LinkedList;
import java.util.Queue;

public class MyStack {
    Queue<Integer> qu1;
    Queue<Integer> qu2;

    public MyStack() {
        qu1 = new LinkedList<>();
        qu2 = new LinkedList<>();
    }

    public void push(int x) {
        if(!qu1.isEmpty()){
            qu1.offer(x);
        }else if(!qu2.isEmpty()){
            qu2.offer(x);
        }else {
            qu1.offer(x);
        }
    }

    public int pop() {
        if (empty()){
            return -1;
        }
        if (!qu2.isEmpty()) {
            int currSize = qu2.size();
            for (int i = 0; i < currSize - 1; i++) {
                int x = qu2.poll();
                qu1.offer(x);
            }
            return qu2.poll();
        }
        if(!qu1.isEmpty()){
            int currSize = qu2.size();
            for (int i = 0; i < qu1.size() - 1; i++) {
                int x = qu1.poll();
                qu2.offer(x);
            }
            return qu1.poll();
        }
        return -1;
    }

    public int top() {
        if(empty()){
            return -1;
        }
        if (!qu2.isEmpty()) {
            int currSize = qu2.size();
            int x = -1;
            for (int i = 0; i < currSize; i++) {
                x = qu2.poll();
                qu1.offer(x);
            }
            return x;
        }
        if(!qu1.isEmpty()){
            int currSize = qu1.size();
            int x = -1;
            for (int i = 0; i < qu1.size(); i++) {
                x = qu1.poll();
                qu2.offer(x);
            }
            return x;
        }
        return -1;
    }

    public boolean empty() {
        return qu1.isEmpty() && qu2.isEmpty();
    }
}
